package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.DeviceOnline;
import com.uwlaser.service.DeviceOnlineService;
import com.uwlaser.service.dto.DeviceOnlineDto;
import com.uwlaser.service.dto.DeviceOnlineQueryParam;
import com.uwlaser.service.mapper.DeviceOnlineMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = DeviceOnlineService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DeviceOnlineServiceImpl extends CommonServiceImpl<DeviceOnlineMapper, DeviceOnline> implements DeviceOnlineService {

    // private final RedisUtils redisUtils;
    private final DeviceOnlineMapper deviceOnlineMapper;

    @Override
    public PageInfo<DeviceOnlineDto> queryAll(DeviceOnlineQueryParam query, Pageable pageable) {
        IPage<DeviceOnline> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DeviceOnline> page = deviceOnlineMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DeviceOnlineDto.class);
    }

    @Override
    public List<DeviceOnlineDto> queryAll(DeviceOnlineQueryParam query){
        return ConvertUtil.convertList(deviceOnlineMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DeviceOnlineDto.class);
    }

    @Override
    public DeviceOnline getById(Integer id) {
        return deviceOnlineMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DeviceOnlineDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), DeviceOnlineDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DeviceOnlineDto resources) {
        DeviceOnline entity = ConvertUtil.convert(resources, DeviceOnline.class);
        return deviceOnlineMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(DeviceOnlineDto resources){
        DeviceOnline entity = ConvertUtil.convert(resources, DeviceOnline.class);
        int ret = deviceOnlineMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return deviceOnlineMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<DeviceOnlineDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DeviceOnlineDto deviceOnline : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("设备编码", deviceOnline.getDeviceCode());
              map.put("设备名称", deviceOnline.getDeviceName());
              map.put("产线编码", deviceOnline.getLineCode());
              map.put("工段编码", deviceOnline.getSectionCode());
              map.put("工位编码", deviceOnline.getStationCode());
              map.put("在线状态", deviceOnline.getIsOnline());
              map.put("创建人", deviceOnline.getCreateBy());
              map.put("创建时间", deviceOnline.getCreateTime());
              map.put("更新人", deviceOnline.getUpdateBy());
              map.put("更新时间", deviceOnline.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
